home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QD3D / 3D Geometry / BoxMooV / sources / BoxMooV_document.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.8 KB  |  259 lines  |  [TEXT/CWIE]

  1. /*  BoxPaint_document.c                                                                            
  2.  
  3.     This contains all the document-specific code.
  4.                                                                                     
  5.     Rick Evans    - September 9, 1996    derived from BoxPaint_document.c
  6.     Michael Bishop - August 21 1996                                                    
  7.     Nick Thompson
  8.     Robert Dierkes                                                                                
  9.     (c)1994-96 Apple computer Inc., All Rights Reserved                                
  10.  
  11. */
  12.  
  13.  
  14. /* --------------------------------------------------------------------
  15. ** Includes
  16. */
  17. #include    "BoxMooV_document.h"
  18. #include    "BoxPaint_Support.h"
  19. #include    "BoxMooV_window.h"
  20. #include    "BoxMooV_texture.h"
  21.  
  22. /* QuickDraw 3D stuff */
  23. #include "QD3DMath.h"
  24. #include "QD3DTransform.h"
  25. #include "QD3DGroup.h"
  26. #include "QD3DShader.h"
  27. #include "QD3DStorage.h"
  28. #include "QD3DIO.h"
  29.  
  30. /* --------------------------------------------------------------------
  31. ** Local Functions
  32. */
  33. void        Document_Init( DocumentHdl theDocument) ;
  34.  
  35.  
  36. /* -------------------------------------------------------------------------------------------
  37. ** Document_Init
  38. ** Sets All Variables and objects common to all documents
  39. */
  40.  
  41. void Document_Init( 
  42.         DocumentHdl theDocument) 
  43. {
  44.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  45.     
  46.     if(theDocument == NULL) goto bail;
  47.     
  48.     (**theDocument).fGroupScale = 1;                
  49.     (**theDocument).fGroupCenter = myOrigin ;            
  50.     
  51.     /*    Get a window for it    */
  52.     if(((**theDocument).fWindow = Window_New()) == NULL) {
  53.         DisposeHandle( (Handle)theDocument ) ;
  54.     }
  55.     else 
  56.     {            
  57.         
  58.         /*  sets up the 3d data for the scene */
  59.         /*  Create view for QuickDraw 3D. */
  60.         (**theDocument).fView = MyNewView( (**theDocument).fWindow ) ;
  61.  
  62.         /*  the main display group: */
  63.         (**theDocument).fModel = MyNewModel() ;
  64.     
  65.         /*  the drawing styles: */
  66.         (**theDocument).fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  67.         (**theDocument).fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  68.         (**theDocument).fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  69.  
  70.         /*  set the rotation matrix the identity matrix */
  71.         Q3Matrix4x4_SetIdentity(&(**theDocument).fRotation);    
  72.             
  73.         /* store a reference to the document record in the window's refcon */
  74.         SetWRefCon ((**theDocument).fWindow, (long)theDocument ) ;
  75.     }
  76. bail:
  77. ;
  78. }
  79.  
  80. /* -------------------------------------------------------------------------------------------
  81. ** Document_GetFromWindow
  82. ** Gets the document associated with a window
  83. */
  84.  
  85. DocumentHdl Document_GetFromWindow( 
  86.         WindowPtr theWindow)
  87. {
  88.     if (theWindow != NULL)
  89.         return    (DocumentHdl) GetWRefCon ( theWindow );
  90.     else return NULL;
  91. }
  92.  
  93.  
  94. /* -------------------------------------------------------------------------------------------
  95. ** Document_Delete
  96. ** Destroys and deletes all the data in a document
  97. */
  98.  
  99. void Document_Delete(
  100.         DocumentHdl theDocument)
  101. {
  102.     if (theDocument != NULL) {
  103.     
  104.         Q3Object_Dispose((**theDocument).fView) ;                /*  the view for the scene */
  105.         Q3Object_Dispose((**theDocument).fModel) ;                /*  object in the scene being modelled */
  106.         Q3Object_Dispose((**theDocument).fInterpolation) ;        /*  interpolation style used when rendering */
  107.         Q3Object_Dispose((**theDocument).fBackFacing) ;            /*  whether to draw shapes that face away from the camera */
  108.         Q3Object_Dispose((**theDocument).fFillStyle) ;            /*  whether drawn as solid filled object or decomposed to components */
  109.         
  110.         Texture_Delete((**theDocument).fTexture);
  111.         
  112.         Window_Delete ( (**theDocument).fWindow ) ;                /*    get rid of the window    */
  113.  
  114.         /*    Do this last    */
  115.         DisposeHandle( (Handle)theDocument ) ;
  116.     }
  117. }
  118.  
  119.  
  120. /* -------------------------------------------------------------------------------------------
  121. **    Document_Draw
  122. **    Draws the document in the current DrawContext
  123. */
  124.  
  125. TQ3Status Document_Draw( DocumentPtr theDocument )
  126. {    
  127.     TQ3Status        status = kQ3Failure;
  128.     
  129.     if(theDocument == NULL) goto bail;
  130.     
  131.     if ((status = Q3View_StartRendering(theDocument->fView)) != kQ3Failure)
  132.     do {
  133.         Document_SubmitScene( theDocument ) ;
  134.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  135.     
  136. bail:
  137.     
  138.     return status ;
  139.  
  140. }
  141.  
  142. /* -------------------------------------------------------------------------------------------
  143. **    Document_New
  144. **    Loads a new Document with a blank (white) canvas for a texture
  145. */
  146.  
  147. DocumentHdl Document_New(
  148.         void)
  149. {
  150.     DocumentHdl theDocument = (DocumentHdl)NewHandle( sizeof(DocumentRec)) ;
  151.     
  152.     if( theDocument != NULL )
  153.     {
  154.  
  155.         HLock( (Handle)theDocument ) ;
  156.             
  157.         /* init the field to some reasonable values */
  158.  
  159.         Document_Init( theDocument) ;
  160.  
  161.         /*    By passing NULL as the first parameter, we tell    the function to
  162.             make a blank offscreen    */
  163.         (**theDocument).fTexture = Texture_New();
  164.  
  165.         /*    Add it to the model    */
  166.         Texture_AddToGroup((**theDocument).fTexture, (**theDocument).fModel) ;
  167.  
  168.         AdjustCamera(*theDocument) ;
  169.  
  170.         HUnlock( (Handle)theDocument ) ;
  171.  
  172.     }
  173.  
  174.     return theDocument ;
  175. }
  176.  
  177. /* -------------------------------------------------------------------------------------------
  178. **    Document_Open
  179. **    Loads a new Document with a Pict for a texture
  180. */
  181.  
  182. DocumentHdl Document_Open(
  183.         void)
  184. {
  185.     DocumentHdl theDocument;
  186.     
  187.     if ( (theDocument = (DocumentHdl)NewHandle( sizeof(DocumentRec))) != NULL )
  188.     {
  189.             
  190.         /* lock the handle and init the field to some reasonable values */
  191.  
  192.         Document_Init(theDocument) ;
  193.  
  194.         (**theDocument).fTexture = Texture_New();
  195.  
  196.         Texture_AddToGroup( (**theDocument).fTexture, (**theDocument).fModel ) ;
  197.  
  198.         HLock( (Handle)theDocument ) ;
  199.  
  200.         AdjustCamera(*theDocument) ;
  201.                 
  202.         HUnlock( (Handle)theDocument ) ;
  203.         
  204.     }    
  205.     
  206.     return theDocument ;
  207.     
  208. }
  209.     
  210. /* -------------------------------------------------------------------------------------------
  211. **    Document_SubmitScene
  212. **    if you make a function like this, you can easily use it inside a
  213. **    Rendering or Picking or BoundingSphere/Box loop
  214. **    (See Document_Draw)
  215. */
  216.  
  217. TQ3Status Document_SubmitScene( 
  218.         DocumentPtr theDocument ) 
  219. {        
  220.     TQ3Vector3D                globalScale;
  221.     TQ3Vector3D                globalTranslate;
  222.     
  223.     globalScale.x = globalScale.y = globalScale.z = theDocument->fGroupScale;
  224.     globalTranslate = *(TQ3Vector3D *)&theDocument->fGroupCenter;
  225.     Q3Vector3D_Scale(&globalTranslate, -1, &globalTranslate);
  226.     Q3Style_Submit(theDocument->fInterpolation, theDocument->fView);
  227.     Q3Style_Submit(theDocument->fBackFacing , theDocument->fView);
  228.     Q3Style_Submit(theDocument->fFillStyle, theDocument->fView);
  229.         
  230.     Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView);
  231.         
  232.     Q3ScaleTransform_Submit(&globalScale, theDocument->fView);
  233.     Q3TranslateTransform_Submit(&globalTranslate, theDocument->fView);
  234.     Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView);
  235.     
  236.     return kQ3Success ;
  237. }
  238.  
  239.  
  240.             
  241. /* -------------------------------------------------------------------------------------------
  242. **    Document_Animate
  243. **    Advance the movie one frame
  244. **    Rotate the box a little
  245. */
  246.  
  247. void Document_Animate(
  248.         DocumentHdl theDocument)
  249. {
  250.     TQ3Matrix4x4        tmp;
  251.     
  252.     Texture_NextFrame((**theDocument).fTexture);
  253.     
  254.     Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.025, 0.03, 0.02);
  255.     Q3Matrix4x4_Multiply(&(**theDocument).fRotation, &tmp,
  256.                             &(**theDocument).fRotation);
  257.  
  258. }
  259.